package com.lagou.solr.position.controller;

import com.lagou.solr.position.dto.SolrQueryDTO;
import com.lagou.solr.position.pojo.Position;
import com.lagou.solr.position.service.SolrDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 职位控制类
 * @author yzh
 */
@RestController
@RequestMapping("position")
public class PositionController {

    @Autowired
    private SolrDemoService<Position> solrDemoService;

    /**
     * 测试职位文档查询
     */
    @GetMapping("queryPosition")
    public List<Position> queryPosition(String positionName) {
        int start = 0;
        int end = 5;
        SolrQueryDTO solrQueryDTO = SolrQueryDTO.builder().query("positionName:"+positionName).start(start).row(end).build();
        List<Position> positionList = solrDemoService.querySolrBeanList(Position.class, solrQueryDTO);
        if(positionList == null || (positionList != null && positionList.size() < 5)) {
            if(positionList == null) {
                positionList = new ArrayList<>();
            }
            end = end - positionList.size();
            solrQueryDTO = SolrQueryDTO.builder().query("positionAdvantage:美女多、员工福利好").start(start).row(end).build();
            positionList.addAll(solrDemoService.querySolrBeanList(Position.class, solrQueryDTO));
        }
        positionList.forEach(position -> {
            System.out.println(position);
        });
        return positionList;
    }
}
